#ifndef QOPENHARMONYPLAYER_H
#define QOPENHARMONYPLAYER_H

#include <multimedia/player_framework/avplayer.h>
#include <napi/native_api.h>
#include <QObject>
#include <QtMultimedia/qmediaplayer.h>

class QNetworkRequest;
struct NativeResourceManager;

class QOpenHarmonyPlayer : public QObject
{
    Q_OBJECT
public:
    QOpenHarmonyPlayer();
    ~QOpenHarmonyPlayer();

    void release();
    void reset();

    int getCurrentPosition();
    int getDuration();
    bool isPlaying();
    int volume();
    bool isMuted();
    qreal playbackRate();

    void play();
    void pause();
    void stop();
    void seekTo(qint32 msec);
    void setMuted(bool mute);
    void setDataSource(const QNetworkRequest &request);
    void prepare();
    void setVolume(int volume);
    bool setPlaybackRate(qreal rate);
    void setDisplay(OHNativeWindow *window);
    OHNativeWindow *display() const;


    void setErrorString(int code, const QString &errorString);
    void setPlayerInfo(AVPlayerOnInfoType type, int32_t extra);

    AVPlayerState state() const;
    void setState(int32_t state);

    static void init(napi_env env, napi_value exports);
    static NativeResourceManager *m_resourceManager;

Q_SIGNALS:
    void error(int code, const QString &errorString);
    void bufferingChanged(qint32 percent);
    void durationChanged(qint64 duration);
    void progressChanged(qint64 progress);
    void stateChanged(qint32 state);
    void videoSizeChanged(qint32 width, qint32 height);
    void audioAvailableChanged(bool available);
    void videoAvailableChanged(bool available);
    void seekableChanged(bool seekable);
    void mediaStatusChanged(QMediaPlayer::MediaStatus status);
private:
    void obtainVideoSize();
private:
    static void onPlayerInfo(OH_AVPlayer *player, AVPlayerOnInfoType type, int32_t extra);
    static void onPlayerError(OH_AVPlayer *player, int32_t errorCode, const char *errorMsg);
private:
    OH_AVPlayer *m_player = nullptr;
    AVPlayerCallback m_playerCallback;
    int m_volume = 0;
    int m_volumeBeforeMute = 0;
    OHNativeWindow *m_window = nullptr;
    bool m_surfaceSetted = false;
    AVPlayerState m_state;
    bool m_isPendingPlaying = false;
};

#endif // QOPENHARMONYPLAYER_H
